/*      $Id: ca_terraquati_canopy_Canopy.c 983 2009-09-01 02:45:42Z phrakt $    */
/*
 * Copyright (c) 2007 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <httpd/httpd.h>

#include "mod_java.h"



/* from src/sbin/httpd/httpd.c */
extern char  *httpd_server_ident;
extern char  *httpd_version_string;


static jint     Canopy_getPid         (JNIEnv *, jclass);
static jstring  Canopy_getServerIdent (JNIEnv *, jclass);
static jstring  Canopy_getVersion     (JNIEnv *, jclass);
static void     Canopy_log            (JNIEnv *, jclass, jint, jstring);
static void     Canopy_restart        (JNIEnv *, jclass);
static void     Canopy_shutdown       (JNIEnv *, jclass);


static const JNINativeMethod Canopy_methods[] = {
	{ "getPid",          "()I",                    Canopy_getPid         },
	{ "getServerIdent",  "()Ljava/lang/String;",   Canopy_getServerIdent },
	{ "getVersion",      "()Ljava/lang/String;",   Canopy_getVersion     },
	{ "log",             "(ILjava/lang/String;)V", Canopy_log            },
	{ "restart",         "()V",                    Canopy_restart        },
	{ "shutdown",        "()V",                    Canopy_shutdown       },
};

/*
 *
 *
 *
 */
int
mod_java_load_Canopy(JNIEnv *env)
{
	jint ret;
	jclass canopy;

	canopy = mod_java_getclass(env, "ca.terraquati.canopy.Canopy", 0);
	if (canopy == NULL)
		return (-1);

	ret = (*env)->RegisterNatives(env, canopy, Canopy_methods,
	    sizeof(Canopy_methods)/sizeof(Canopy_methods[0]));
	if (ret != JNI_OK) {
		canopy_log(CNP_LOG_ERR, "failed to register native methods: %s",
		    mod_java_jnierr(ret));
		return (-1);
	}

	(*env)->DeleteLocalRef(env, canopy);

	return (0);
}

/*
 *
 *
 */
static jint
Canopy_getPid(JNIEnv *env, jclass class)
{
	jint jpid = (jint)getpid();

	return (jpid);
}

/*
 *
 *
 */
static jstring
Canopy_getServerIdent(JNIEnv *env, jclass class)
{
	jstring ident;

	ident = mod_java_string_ctoj(env, httpd_server_ident);

	return (ident);
}

/*
 *
 *
 */
static jstring
Canopy_getVersion(JNIEnv *env, jclass class)
{
	jstring version;

	version = mod_java_string_ctoj(env, httpd_version_string);

	return (version);
}

/*
 *
 *
 */
static void
Canopy_log(JNIEnv *env, jclass class, jint priority,
    jstring msg)
{
	const char *logmsg = "JAVA LOG MESSAGE";

	canopy_log(CNP_LOG_INFO, logmsg);
}


/*
 *
 *
 */
static void
Canopy_restart(JNIEnv *env, jclass class)
{
	httpd_restart();
}

/*
 *
 *
 */
static void
Canopy_shutdown(JNIEnv *env, jclass class)
{
	httpd_shutdown();
}

/*
 *
 *
 */
static void
Canopy_addHandler(JNIEnv *env, jclass class,
    jobject phase, jobject hdlr)
{
}

/*
 *
 *
 */
static void
Canopy_removeHandler(JNIEnv *env, jclass class, jobject phase, jobject hdlr)
{
}
